package models;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;

import play.db.jpa.Model;
import play.libs.Codec;
@Entity
public class User extends Model {

	public String email;
	public String password;
	public String name;
	public String title;
	public String introduction;
	//public long experience用户的经验值（活跃度）
	@OneToOne
	public Level level;
	public String photo;
	
	@OneToMany
	public List<Link> links;
	public String uuid;
	
	
	//级联跟新与保存
	@ManyToMany(mappedBy="author",cascade={CascadeType.MERGE,CascadeType.PERSIST})
	public List<App> apps;
	
	public User(String email){
		this.email=email;
		this.uuid=Codec.UUID();
		this.links=new ArrayList<Link>();
		this.apps=new ArrayList<App>();
		
	}
	
	public boolean checkPassword(String password){
		return this.password.equals(Codec.hexMD5(password));
	}
	
	public User addLink(String title,String url){
		//注意对link的保存
		Link link=new Link(title,url).save();
		this.links.add(link);
		this.save();
		return this;
	}
	
	public User setLevel(Level level){
		this.level=level;
		this.save();
		return this;
	}
	
}
